package com.gh.controller.api;

        import com.gh.common.result.R;
        import com.gh.entity.Lend;
        import com.gh.service.LendService;
        import com.gh.service.UserAccountService;
        import com.gh.utils.JwtUtils;
        import com.gh.vo.InvestVo;
        import io.swagger.annotations.Api;
        import io.swagger.annotations.ApiOperation;
        import io.swagger.annotations.ApiParam;
        import lombok.extern.slf4j.Slf4j;
        import org.springframework.web.bind.annotation.*;

        import javax.annotation.Resource;
        import javax.servlet.http.HttpServletRequest;
        import java.math.BigDecimal;
        import java.util.List;
        import java.util.Map;

/**
 * @Author Eric
 * @Date 2021/10/13 21:04
 * @Version 1.0
 */
@Api(tags = "标的信息")
@RestController
@RequestMapping("/api/core/lend")
@Slf4j
public class LendController {

    @Resource
    private LendService lendService;

    @Resource
    private UserAccountService userAccountService;

    @ApiOperation("获取标的列表")
    @GetMapping("/list")
    public R list() {
        List<Lend> lendList = lendService.selectList();
        return R.ok().data("lendList", lendList);
    }

    @ApiOperation("获取标的信息")
    @GetMapping("/show/{id}")
    public R show(@ApiParam(value = "标的id", required = true)
                  @PathVariable Long id) {
        Map<String, Object> lendDetail = lendService.show(id);
        return R.ok().data("lendDetail", lendDetail);
    }

    @ApiOperation("查询账户余额")
    @GetMapping("/auth/getAccount")
    public R getAccount(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        BigDecimal account = userAccountService.getAccount(userId);
        return R.ok().data("account", account);
    }

    @ApiOperation("计算投资收益")
    @GetMapping("/getInterestCount/{invest}/{yearRate}/{totalMonth}/{returnMethod}")
    public R getInterestCount(@ApiParam(value = "投资金额", required = true)
                              @PathVariable BigDecimal invest,
                              @ApiParam(value = "年化收益", required = true)
                              @PathVariable BigDecimal yearRate,
                              @ApiParam(value = "期数", required = true)
                              @PathVariable Integer totalMonth,
                              @ApiParam(value = "还款方式")
                              @PathVariable Integer returnMethod) {
        BigDecimal interestCount = lendService.getInterestCount(invest,yearRate,totalMonth,returnMethod);
        return R.ok().data("interestCount",interestCount);
    }
}
